package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BdmOrder;
import com.ruoyi.system.domain.BdmSignIn;
import com.ruoyi.system.domain.vo.applet.UserIntegralVo;
import com.ruoyi.system.service.IBdmOrderService;
import com.ruoyi.system.service.IBdmSignInService;
import com.ruoyi.system.service.IIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 积分Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
public class IntegralServiceImpl implements IIntegralService
{

    @Autowired
    private IBdmSignInService bdmSignInService;

    @Autowired
    private IBdmOrderService bdmOrderService;


    /**
     * 查询用户积分列表
     * @return
     */
    @Override
    public List<UserIntegralVo> selectIntegralList() {
        // 查询签到表
        Long sysUserId = SecurityUtils.getUserId();

        BdmSignIn bdmSignIn = new BdmSignIn();
        bdmSignIn.setSysUid(sysUserId);
        List<UserIntegralVo> signInList = Optional.ofNullable(bdmSignInService.selectBdmSignInList(bdmSignIn))
                .orElse(Collections.emptyList())
                .stream().filter(signin -> {
                    String integral = signin.getScore();
                    return integral != null && !integral.equals("0");
                })
                .map(score -> {
                    UserIntegralVo vo = new UserIntegralVo();
                    vo.setIntegralNum(score.getScore());
                    vo.setGainOrUseId(String.valueOf(score.getId()));
                    vo.setGainOrUseTime(score.getCreateTime());
                    vo.setGainType("签到");
                    vo.setGainOrUse("获取");
                    return vo;
                })
                .collect(Collectors.toList());

        BdmOrder bdmOrder = new BdmOrder();
        bdmOrder.setUserId(sysUserId);
        bdmOrder.setOrderStatus(4L);
        List<BdmOrder> orderList = Optional.ofNullable(bdmOrderService.selectBdmOrderList(bdmOrder))
                .orElse(Collections.emptyList());

        // 订单获得积分
        List<UserIntegralVo> gainOrderList = orderList.stream().filter(order -> {
            String integral = order.getGainIntegral();
            return integral != null && !integral.equals("0");
        }).map( order -> {
            UserIntegralVo vo = new UserIntegralVo();
            vo.setIntegralNum(order.getGainIntegral());
            vo.setGainOrUseId(order.getId());
            vo.setGainOrUseTime(order.getReceiveTime());
            vo.setGainType("订单");
            vo.setGainOrUse("获取");
            return vo;
        }).collect(Collectors.toList());
        signInList.addAll(gainOrderList);

        // 订单使用积分
        List<UserIntegralVo> useOrderList = orderList.stream().filter(order -> {
            String integral = order.getDeductIntegral();
            return integral != null && !integral.equals("0");
        }).map( order -> {
            UserIntegralVo vo = new UserIntegralVo();
            vo.setIntegralNum(order.getGainIntegral());
            vo.setGainOrUseId(order.getId());
            vo.setGainOrUseTime(order.getReceiveTime());
            vo.setGainType("订单");
            vo.setGainOrUse("使用");
            return vo;
        }).collect(Collectors.toList());
        signInList.addAll(useOrderList);

        // 排序
        signInList = signInList.stream()
                .sorted(Comparator.comparing(UserIntegralVo::getGainOrUseTime).reversed())
                .collect(Collectors.toList());

        // 查询订单表
        return signInList;
    }
}
